include nesdefines.s

; ---------- ZERO PAGE -------------
ENUM 0
	VM_BTM: .dsb $20
	VM_TOP: .db 0
	PAL_BUF: .dsb $20
	FRAMES: .db 0
	FRAMECNT=FRAMES
	PPU_CTRL_VAR: .db 0
	PPU_MASK_VAR: .db 0
	SCROLL_X: .db 0
	SCROLL_Y: .db 0

	SRC: .dw 0
	OAM_ATTR_EOR: .db 0

	TEMP0: .db 0
	TEMP1: .db 0
	TEMP2: .db 0
	TEMP3: .db 0
	TEMP4: .db 0
	TEMP5: .db 0
	TEMP6: .db 0
	TEMP7: .db 0
	TEMP8: .db 0
	TEMP9: .db 0
	TEMP10: .db 0
	TEMP11: .db 0

	OBJ_INDEX: .db 0  ; current object #, offset into interleaved array of byte properties

	PAD_STATE: .db 0
	PAD_STATEL: .db 0

; Player stuff
	FIXP_STRIDE = 1
	PLAYER_DIR: .db 0
	PLAYER_X: .dw 0
	PLAYER_Y: .dw 0
	PLAYER_VX: .dw 0
	PLAYER_VY: .dw 0

	ObjectX = PLAYER_X
	ObjectY = PLAYER_Y
	ObjectVX = PLAYER_VX
	ObjectVY = PLAYER_VY

   PLAYER_CNT: .db 0
	PLAYER_ACNT: .db 0
ENDE
; ----------------------------------

; this is the header bitch!  don't move it!
include header.s
; PC lands here. no JMP

	poke PPU_CTRL, $00
	poke PPU_MASK, $00

	; PPU initialization

	ldy #31
-	lda testpal,y
	sta PAL_BUF,y
	dey
	bpl -

	ldy #0
-	lda testoam,y
	sta OAM_BUF,y
	iny
	bne -


	lda #$20
	sta PPU_ADDR
	lda #$00
	sta PPU_ADDR

-
	lda testnam,y
	sta PPU_DATA
	iny
	bne -
-
	lda testnam+256,y
	sta PPU_DATA
	iny
	bne -
-
	lda testnam+512,y
	sta PPU_DATA
	iny
	bne -
-
	lda testnam+768,y
	sta PPU_DATA
	iny
	bne -



	poke PPU_CTRL, $B0  ; 8x16 sprites, spr patterns in bank 0, bg patterns in bank 1
	poke PPU_MASK, $1E  ; $7e for yellowed appearance - too dark? need to see on real hardware


	lda #0
; Endless loop
loop_start:
	cmp FRAMECNT
	beq loop_start
	lda FRAMECNT
	pha

	poke PPU_MASK, $3E

	lda #0
	jsr poll_pad

; fast clear oambuf
	ldy #40-4
	lda #$ff
-
	sta OAM_BUF,y
	sta OAM_BUF+64,y
	sta OAM_BUF+128,y
	sta OAM_BUF+192,y
	dey
	dey
	dey
	dey
	bpl -

	poke PPU_MASK, $9E

	jsr script

	poke PPU_MASK, $1E

	pla
	jmp loop_start

;------------------------------------------------------------------------------------------------

include bytecode_y_s.s
include metasprites.s
include bytecode_opcodes.s
include map.s
include script.s





testoam:
.pad testoam+256,$ff


vblank:

	rts



testpal:
incbin bg2.pal
incbin spr.pal

testnam:
incbin bg2.nam



org $FFFA
	.dw nmi	;$fffa vblank nmi
	.dw start	;$fffc reset
	.dw irq	;$fffe irq / brk


; CHR ROM
incbin spr.chr
incbin bg2.chr